﻿
Imports std

Public Class commonStringsPackage
    Private languages As map(Of String, strings) = Nothing
    Public Const defaultLanguage As String = "en"

    Public Sub insertStrings(ByVal s As strings)
        assert(Not s Is Nothing, "s is a nothing strings instance.")
        'do not care about cases, but map<string, strings> keep cases
        languages(s.language().ToLower()) = s
    End Sub

    Public Sub clear()
        languages.clear()
    End Sub

    'full match here, so may return nothing
    Private Function stringsImpl(ByVal lang As String) As strings
        If languages.size() = 0 OrElse isemptystring(lang) Then
            Return Nothing
        End If

        'consistance with insertStrings
        lang = lang.ToLower()
        Dim it As map(Of String, strings).iterator = Nothing
        it = languages.find(lang)

        If it <> languages.end() Then
            Return (+it).second
        Else
            Return Nothing
        End If
#If RETIRED Then
        If it = languages.end() Then
            it = languages.find(defaultLanguage)
            If it = languages.end() Then
                it = languages.begin()
            End If
        End If

        If it <> languages.end() Then
            Return (+(it)).second
        Else
            Return Nothing
        End If
#End If
    End Function

    Public Function strings(ByVal lang As String) As strings
        If isemptystring(lang) Then
            Return stringsImpl(defaultLanguage)
        Else
            Dim rtn As strings = Nothing
            Dim market As String = Nothing
            Dim language As String = Nothing
            languageSeparate(lang, market, language)
            'full match first
            rtn = stringsImpl(lang)
            If rtn Is Nothing AndAlso Not isemptystring(market) Then
                rtn = stringsImpl(market)
            End If
            If rtn Is Nothing AndAlso Not isemptystring(language) Then
                rtn = stringsImpl(language)
            End If
            If rtn Is Nothing Then
                rtn = stringsImpl(defaultLanguage)
            End If

            Return rtn
        End If
    End Function

#If Not (PocketPC OrElse Smartphone) Then
    Public Overridable Function strings() As strings
        Return strings(envs.currentLanguage())
    End Function
#End If

    Public Sub New()
        languages = New map(Of String, strings)()
    End Sub
End Class

Public MustInherit Class strings
    Protected m As map(Of Int64, String) = Nothing
    Public Const MISSINGSTRING As String = "[MISSING_STRING]"

    Public MustOverride Function language() As String

    Default Public ReadOnly Property [string](ByVal index As Int64) As String
        Get
            Dim it As map(Of Int64, String).iterator = Nothing
            it = m.find(index)
            If it = m.end() Then
                Return MISSINGSTRING + "{" + Me.GetType().FullName + "}"
            Else
                Return (+it).second
            End If
        End Get
    End Property

    Public Sub def(ByVal index As Int64, ByVal s As String)
        m(index) = s
    End Sub

    Public Sub New()
        m = New map(Of Int64, String)()
    End Sub
End Class
